//
//  HPSlider.m
//  Handpic_Pro
//
//  Created by 刘功武 on 2020/3/6.
//  Copyright © 2020年 北京汉德图像. All rights reserved.
//

#import "HPSlider.h"
/** 滑块宽高 */
#define Slider_W_H 18
@interface HPSlider ()
/** 轨道背景 */
@property (nonatomic, strong) UIView *backView;
/** 轨道前景 */
@property (nonatomic, strong) UIView *trackView;
/** 滑块 */
@property (nonatomic, strong) UIView *thumbView;
@end

@implementation HPSlider

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectMake(Slider_W_H/2, (self.height-5)/2, self.width-Slider_W_H, 5)];
        _backView.backgroundColor = c_ffffff;
        _backView.layer.cornerRadius = _backView.height/2;
        _backView.layer.masksToBounds = YES;
        [self addSubview:_backView];
    }
    return _backView;
}

- (UIView *)trackView {
    if (!_trackView) {
        _trackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, self.backView.height)];
        _trackView.backgroundColor = RANDOMCOLOR();
        [self.backView addSubview:_trackView];
    }
    return _trackView;
}

- (UIView *)thumbView {
    if (!_thumbView) {
        _thumbView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Slider_W_H, Slider_W_H)];
        _thumbView.centerY = self.backView.centerY;
        _thumbView.layer.cornerRadius = _thumbView.height/2;
        _thumbView.layer.masksToBounds = YES;
        _thumbView.backgroundColor = c_ffffff;
        _thumbView.userInteractionEnabled = YES;
        [_thumbView addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]];
    }
    return _thumbView;
}

- (instancetype)initWithFrame:(CGRect)frame max_score:(int)max_score {
    if (self = [super initWithFrame:frame]) {
        self.max_score = max_score;
        [self defaultConfig];
    }
    return self;
}

- (instancetype)initWithMax_score:(int)max_score{
    if (self = [super init]) {
        self.max_score = max_score;
        [self defaultConfig];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.max_score = 0;
        [self defaultConfig];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.max_score = 0;
        [self defaultConfig];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self addSubview:self.thumbView];
    
    if (self.current_score>self.max_score) {
        self.current_score = self.max_score;
    }
    //刷新视图
    [self reloadViewWithThumbCeneterX:self.current_score / self.max_score * self.backView.width];
}

- (void)defaultConfig {
    _current_score = 0;
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]];
}

#pragma mark -点击滑动条
- (void)handleTap:(UITapGestureRecognizer *)sender {
    //刷新视图
    [self reloadViewWithThumbCeneterX:[sender locationInView:self].x];
}

#pragma mark -滑动滑块
- (void)handlePan:(UIPanGestureRecognizer *)sender {
    //获取偏移量
    CGFloat moveX = [sender translationInView:self].x;
    //重置偏移量，避免下次获取到的是原基础的增量
    [sender setTranslation:CGPointMake(0, 0) inView:self];
    //计算当前中心值
    CGFloat centerX = _thumbView.centerX + moveX;
    //防止瞬间大偏移量滑动影响显示效果
    if (centerX <= self.backView.origin.x) centerX = self.backView.origin.x;
    if (centerX >= self.backView.width) centerX = self.backView.width;
    //刷新视图
    [self reloadViewWithThumbCeneterX:centerX];
}

- (void)reloadViewWithThumbCeneterX:(CGFloat)thumbCeneterX {
    
    //更新滑块位置
    self.thumbView.centerX = thumbCeneterX;
    if (self.thumbView.centerX <= self.backView.origin.x) {
        self.thumbView.centerX = self.backView.origin.x;
        thumbCeneterX = 0;
    }else if (self.thumbView.centerX >= self.backView.width) {
        self.thumbView.centerX = self.backView.width;
        thumbCeneterX = self.backView.width;
    }
    //更新轨道前景色
    self.trackView.width = thumbCeneterX;
    //更新标签内容
    NSString *coreValue = [NSString stringWithFormat:@"%.f", thumbCeneterX / self.backView.width * self.max_score];
    if ([self.delegate respondsToSelector:@selector(sliderDidChangeValue:)]) {
        [self.delegate sliderDidChangeValue:coreValue];
    }
}

@end
